package com.colin.app.activity.senior.scroll;

import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.colin.app.R;
import com.colin.app.activity.senior.SeniorContract;
import com.colin.app.activity.senior.SeniorPresenterImpl;
import com.colin.app.app.AppActivity;
import com.colin.app.bean.DemoBean;
import com.colin.library.help.LogHelp;
import com.colin.library.view.help.MetricsHelp;
import com.nineoldandroids.view.ViewHelper;

public class ScrollActivity extends AppActivity implements SeniorContract.SeniorView {
    private SeniorContract.SeniorPresenter mSeniorPresenter = null;
    private View view_scroll_to;
    private View view_scroll_by;
    private View view_scroll_anim;
    private View view_scroll_params;
    private View view_scroll_custom;
    private int mLastX = 0;
    private int mLastY = 0;

    @Override
    protected void onDestroy() {
        if (null != mSeniorPresenter) {
            mSeniorPresenter.destroy();
            mSeniorPresenter = null;
        }
        super.onDestroy();
    }

    @Override
    public void initPresenter() {
        DemoBean demoBean = getIntent().getExtras().getParcelable("clazz");
        mSeniorPresenter = new SeniorPresenterImpl(this, this, demoBean);
    }

    @Override
    public int initViewLayoutID() {
        return R.layout.activity_scroll;
    }

    @Override
    public void initView() {
        this.view_scroll_to = this.findViewById(R.id.view_scroll_to);
        this.view_scroll_by = this.findViewById(R.id.view_scroll_by);
        this.view_scroll_anim = this.findViewById(R.id.view_scroll_anim);
        this.view_scroll_params = this.findViewById(R.id.view_scroll_params);
        this.view_scroll_custom = this.findViewById(R.id.view_scroll_custom);
    }

    @Override
    public void initData() {
        mSeniorPresenter.setData();
        mLastX = (int) view_scroll_custom.getX();
        mLastY = (int) view_scroll_custom.getY();

        String.valueOf("mLastX==" + String.valueOf(mLastX));
        String.valueOf("mLastY==" + String.valueOf(mLastY));
    }

    @Override
    public void initListener() {
        this.view_scroll_custom.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getRawX();
                int y = (int) event.getRawY();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int deltaX = x - mLastX;
                        int deltaY = y - mLastY;
                        LogHelp.d("move:" + "deltaX-->>" + String.valueOf(deltaX) + "\tdeltaY-->>" + String.valueOf(deltaY));
                        int translateX = (int) (ViewHelper.getTranslationX(v) + deltaX);
                        int translateY = (int) (ViewHelper.getTranslationY(v) + deltaY);
                        ViewHelper.setTranslationX(v, translateX);
                        ViewHelper.setTranslationY(v, translateY);
                        break;
                }
                mLastX = x;
                mLastY = y;
                return true;
            }
        });
    }

    /**
     * scrollTo/scrollBy  :简单操作 适合控件内容滑动
     *
     * @param view
     */
    public void scrollTo(View view) {
        view.scrollTo(MetricsHelp.px2dp(this, 100), 0);
        view_scroll_to.scrollTo(MetricsHelp.px2dp(this, 100), 0);
    }

    public void scrollBy(View view) {
        view.scrollBy(MetricsHelp.px2dp(this, 100), 0);
        view_scroll_by.scrollBy(MetricsHelp.px2dp(this, 100), 0);
    }

    /**
     * anim 操作简单：主要是没有交互的View和实现复杂的动画效果
     *
     * @param view
     */
    public void animTranslate(View view) {
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_translate);
        view.startAnimation(animation);
        view_scroll_anim.startAnimation(animation);
    }

    /**
     * 修改参数 ：操作有点复杂：适合与有交互的View
     *
     * @param view
     */
    public void scrollParams(View view) {
        ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        marginLayoutParams.leftMargin += MetricsHelp.px2dp(this, 100);
        //更新布局
        view.requestLayout();

        ViewGroup.MarginLayoutParams viewMarginLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        viewMarginLayoutParams.leftMargin += MetricsHelp.px2dp(this, 100);
        //更新布局
        view_scroll_params.setLayoutParams(viewMarginLayoutParams);

    }

}
